//
//  LGOutpatientRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/1.
//  报告中心 -- 门诊病历

#import "LGOutpatientRecordController.h"

//报告详情
#import "LGInspectReportDetailController.h"
//门诊病历
#import "LGClinicMedicalRecordController.h"
//出院证明
#import "LGRecordHospitalController.h"
#import "LGRecordHospitalController2.h"
//PCI报告单
#import "LGPCIReportDetailController.h"

#import "LGReportCenterHeaderView.h"
#import "LGOutpatientRecordCell.h"
#import "LGCheckPhotoTool.h"
#import "LGPickerViewMax.h"

@interface LGOutpatientRecordController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UIView *topLine;
/**   */
@property (nonatomic , strong)UIButton *yearButton;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , copy)NSString *type;
/**   */
@property (nonatomic , copy)NSString *year;
/**   */
@property (nonatomic , strong)NSMutableArray *yearArray;
/**   */
@property (nonatomic , strong)NSArray *typeArray;
/**   */
@property (nonatomic , strong)NSArray *typeDataArray;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , assign)CGPoint checkPoint;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;
@end

@implementation LGOutpatientRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.type = @"";
    self.year = @"";
    self.yearArray = [NSMutableArray array];
    [self.yearArray addObject:@"全部时间"];
    NSInteger currentYear = [[self.formatter stringFromDate:[NSDate date]] integerValue];
    for (NSInteger i=currentYear; i>=2015; i--) {
        [self.yearArray addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    [self.view addSubview:self.yearButton];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    self.emptyView.backgroundColor = [UIColor clearColor];
}

//模块被选中
-(void)viewWillAppearAction{
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Visitrecords/inspectionlist" parameters:@{@"data_type":@"3",@"timeframe":self.year,@"patient_id":self.patientid} withSign:YES
                  success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:viewPix(30)];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:viewPix(30)];
        }
    }];
}

-(void)selectButtonAction:(UIButton *)sender{
    LGPickerViewMax *pickerView = [[LGPickerViewMax alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = @[self.yearArray];
    if(self.year.length>0){
        pickerView.selectStr = self.year;
    }else{
        pickerView.selectStr = @"全部时间";
    }
    [self.view.window addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        NSString *result = resultArray[0];
        weakSelf.year = [result isEqualToString:@"全部时间"]?@"":result;
        NSString *title = [result isEqualToString:@"全部时间"]?@"全部时间":[NSString stringWithFormat:@"%@年",result];
        [weakSelf.yearButton setTitle:title forState:UIControlStateNormal];
        [weakSelf requestData];
    };
}



-(void)checkPhotoWithPoint:(CGPoint)point index:(NSInteger)index photoArray:(NSArray *)array{
    self.checkPoint = point;
    LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    photoView.picArray = array;
    [photoView showViewAnimation:point index:index];
    [[UIApplication sharedApplication].keyWindow addSubview:photoView];
}

//删除报告
-(void)deleteReportAction:(NSDictionary *)dataDic{
    NSString *content = [NSString stringWithFormat:@"确定要删除%@的%@吗？",dataDic[@"datestr"],dataDic[@"visit_record_type_str"]];
    __block NSString *reportid = LGNSString(dataDic[@"record_id"]);
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:content sureAction:^{
        [RequestUtil withPOST:@"/doctor/Visitrecords/visitDel" parameters:@{@"record_id":reportid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf requestData];
            }
        } error:^(NSError *error) {
            
        }];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *itemArray = self.dataArray[section][@"dlists"];
    return itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *itemArray = self.dataArray[indexPath.section][@"dlists"];
    LGOutpatientRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(itemArray.count == 1){
        cell.type = @"single";
    }else if (indexPath.row == 0){
        cell.type = @"top";
    }else if (indexPath.row == itemArray.count-1){
        cell.type = @"bottom";
    }else{
        cell.type = @"middle";
    }
    cell.dataDic = itemArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.section][@"dlists"][indexPath.row];
    //检验检查 -- 详情
    LGInspectReportDetailController *controller = [[LGInspectReportDetailController alloc]init];
    controller.recordid = LGNSString(tempDic[@"record_id"]);
    controller.patientid = self.patientid;
    controller.data_type = @"3";
    controller.reportDate = LGNSString(tempDic[@"report_date"]);
    [self.navigationController pushViewController:controller animated:YES];
    
    // 旧版 就诊记录 -- 详情
//    LGOutRecordDetailController *controller = [[LGOutRecordDetailController alloc]init];
//    controller.recordid = LGNSString(tempDic[@"items"][indexPath.row][@"record_id"]);
//    controller.patientid = self.patientid;
//    controller.reportDate = LGNSString(tempDic[@"date"]);
//    [self.navigationController pushViewController:controller animated:YES];
//    if ([tempDic[@"type"] isEqualToString:@"his"]) {
//        if ([tempDic[@"visit_record_type"] integerValue] == 1){
//           //1：门诊病历
//           LGClinicMedicalRecordController *controller = [[LGClinicMedicalRecordController alloc]init];
//           controller.recordId = LGNSString(tempDic[@"record_id"]);
//           [self.navigationController pushViewController:controller animated:YES];
//       }else if ([tempDic[@"visit_record_type"] integerValue] == 2) {
//            //2：出院病情
//           if([LGNSString(tempDic[@"version"]) isEqualToString:@"1"]){
//               LGRecordHospitalController *controller = [[LGRecordHospitalController alloc]init];
//               controller.recordId = LGNSString(tempDic[@"record_id"]);
//               [self.navigationController pushViewController:controller animated:YES];
//           }else{
//               LGRecordHospitalController2 *controller = [[LGRecordHospitalController2 alloc]init];
//               controller.recordId = LGNSString(tempDic[@"record_id"]);
//               [self.navigationController pushViewController:controller animated:YES];
//           }
//
//       }else if([tempDic[@"visit_record_type"] integerValue] == 501 || [tempDic[@"visit_record_type"] integerValue] == 502 || [tempDic[@"visit_record_type"] integerValue] == 503){
//           //501、502、503  三种报告
//           LGPCIReportDetailController *controller = [[LGPCIReportDetailController alloc]init];
//           controller.recordid = LGNSString(tempDic[@"record_id"]);
//           controller.type = LGNSString(tempDic[@"visit_record_type"]);
//           [self.navigationController pushViewController:controller animated:YES];
//       }
//    }else if ([tempDic[@"type"] isEqualToString:@"doctor"]){
//        //自己添加的报告
//        LGAddOutpatientRecordController *controller = [[LGAddOutpatientRecordController alloc]init];
//        controller.patientid = self.patientid;
//        controller.reportid = LGNSString(tempDic[@"record_id"]);
//        [self.navigationController pushViewController:controller animated:YES];
//        __weak typeof(self) weakSelf = self;
//        controller.addReportSuccess = ^{
//            [weakSelf viewWillAppearAction];
//        };
//    }else{
//        //患者上传的图片
//        NSArray *photoArray = tempDic[@"images"];
//        self.photoView.title = LGNSString(tempDic[@"visit_record_type_str"]);
//        [self checkPhotoWithPoint:CGPointMake(Screen_W/2.0, Screen_H/2.0) index:0 photoArray:photoArray];
//    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGReportCenterHeaderView *headView = [[LGReportCenterHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    headView.content = LGNSString(self.dataArray[section][@"datestr"]);
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, viewPix(45), Screen_W, self.view.bounds.size.height-viewPix(45));
    self.emptyView.frame = CGRectMake(0, viewPix(45), Screen_W, self.view.bounds.size.height-viewPix(45));
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"YYYY";
    }
    return _formatter;
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
        _topLine.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    }
    return _topLine;
}


-(UIButton *)yearButton{
    if(!_yearButton){
        _yearButton = [UIButton buttonWithTitle:@"全部时间" titleFont:LGFont(14) textColor:@"#282828" imageName:@""];
        [_yearButton setShadoWithColor:@"#99B6D7" offset:CGSizeMake(0, 2) opacity:0.15 radius:8];
        _yearButton.frame = CGRectMake(viewPix(16), viewPix(5), Screen_W-viewPix(32), viewPix(34));
        _yearButton.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(12), 0, viewPix(35));
        _yearButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _yearButton.backgroundColor = [UIColor whiteColor];
        [_yearButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blackTipDown16"]];
        [_yearButton addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.yearButton).offset(-viewPix(12));
            make.centerY.equalTo(self.yearButton);
            make.width.height.equalTo(@(viewPix(16)));
        }];
    }
    return _yearButton;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(45), Screen_W, Screen_H-topBarHeight-viewPix(89)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGOutpatientRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(46);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
